﻿using MyTest;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Collection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 关联性泛型集合（key-Value）

        #region Dictionary（哈希表）
        Dictionary<string, Product2> dictProducts = new Dictionary<string, Product2>();
        dictProducts.Add("t1", new Product2());
        dictProducts.Add("t2", new Product2());
        dictProducts.Add("t3", new Product2());
        Response.Write("字典集合大小:" + dictProducts.Count + "<br>");
        Product2 p;
        Response.Write(dictProducts.TryGetValue("t1", out p) + "<br>");
        StringBuilder sb = new StringBuilder();
        //dictProducts.Values; 字典值的集合
        foreach (var key in dictProducts.Keys)
        {
            sb.Append(key + "\t");
        }
        Response.Write(sb.ToString() + "<br>");
        #endregion

        #region SortedDictionary（二叉树）

        SortedDictionary<string, Product2> sDictProducts = new SortedDictionary<string, Product2>();
        sDictProducts.Add("t1", new Product2());
        sDictProducts.Add("t2", new Product2());
        sDictProducts.Add("t4", new Product2());
        sDictProducts.Add("t3", new Product2());
        Response.Write("字典集合大小:" + sDictProducts.Count + "<br>");
        Response.Write(dictProducts.ContainsKey("t10"+"<br>"));
        StringBuilder sb2 = new StringBuilder();


        foreach (var key in dictProducts.Keys)
        {
            sb2.Append(key + "\t");
        }
        Response.Write(sb2.ToString() + "<br>");


        #endregion

        #region SortedList（数组）

        SortedList<string, Product2> sListProducts = new SortedList<    , Product2>();
        sListProducts.Add("t3", new Product2());
        sListProducts.Add("t1", new Product2());
        sListProducts.Add("t2", new Product2());
        StringBuilder sb3 = new StringBuilder();
        foreach (var key in dictProducts.Keys)
        {
            sb3.Append(key + "\t");
        }
        Response.Write(sb3.ToString() + "<br>");
        #endregion

        #endregion

        #region 非关联性泛型集合 
        #region List（数组）
        List<Product2> product2s = new List<Product2>();
        product2s.Add(new Product2());
        Response.Write("集合个数：" + product2s.Count + "<br>");
        Response.Write("第一个元素的hash值：" + product2s[0].GetHashCode() + "<br>");

        #endregion

        #region LinkedList（双向链表）
        LinkedList<Product2> product3 = new LinkedList<Product2>();
        product3.AddLast(new Product2("aa", "type"));
        product3.AddFirst(new Product2("bb", "type"));
        
        StringBuilder sb4 = new StringBuilder();
        foreach (var product in product3)
        {
            sb4.Append(product.Name + "\t");
        }
        Response.Write(sb4.ToString() + "<br>");

        #endregion

        #region HashSet（哈希表）“相同的对象：判断两个对象相等”
        HashSet<Product2> hsproducts = new HashSet<Product2>();
        hsproducts.Add(new Product2("cc", "type"));
        hsproducts.Add(new Product2("aa", "type"));
        hsproducts.Add(new Product2("aa", "type"));
        hsproducts.Add(new Product2("bb", "type"));
        StringBuilder sbS = new StringBuilder();
        foreach (var product in hsproducts)
        {
            sbS.Append(product.Name + "\t");
        }
        Response.Write(sbS.ToString() + "<br>");

        #endregion
       
        #region  （二叉树）
        SortedSet<Product2> product4 = new SortedSet<Product2>();
        product4.Add(new Product2("cc", "type"));
        product4.Add(new Product2("aa", "type"));
        product4.Add(new Product2("aa", "type"));
        product4.Add(new Product2("bb", "type"));
        StringBuilder sb6 = new StringBuilder();
        foreach (var product in product4)
        {
            sb6.Append(product.Name + "\t");
        }
        Response.Write(sb6.ToString() + "<br>");
        #endregion

        #region  Stack（栈）
        Stack<Product2> sproduct2s = new Stack<Product2>();
        sproduct2s.Push(new Product2("aa", "type"));
        sproduct2s.Push(new Product2("bb", "type"));
        Response.Write("栈顶元素" + sproduct2s.Peek().Name + "<br>");
        Response.Write("栈内元素个数" + sproduct2s.Count + "<br>");
        Response.Write("出栈" + sproduct2s.Pop().Name + "<br>");
        Response.Write("栈顶元素" + sproduct2s.Peek().Name + "<br>");
        Response.Write("栈内元素个数" + sproduct2s.Count + "<br>");

        #endregion

        #region Queue（队列）
        Queue<Product2> qproduct2s = new Queue<Product2>();
        qproduct2s.Enqueue(new Product2("aa", "type"));
        qproduct2s.Enqueue(new Product2("bb", "type"));
        Response.Write("队列头部元素" + qproduct2s.Peek().Name + "<br>");
        Response.Write("队列元素个数" + qproduct2s.Count + "<br>");
        Response.Write("队列出队元素" + qproduct2s.Dequeue().Name + "<br>");
        Response.Write("队列头部元素" + qproduct2s.Peek().Name + "<br>");
        Response.Write("队列元素个数" + qproduct2s.Count + "<br>");

        #endregion
        #endregion
    }




}